package com.lzb.newloan;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lzb.newloan.adapter.MarketAdapter;
import com.lzb.newloan.pojo.MarketPojo;
import com.lzb.newloan.utils.AlertDialog;
import com.lzb.newloan.utils.BaseActivity;
import com.lzb.newloan.utils.Config;
import com.lzb.newloan.utils.HttpUtils;
import com.lzb.newloan.utils.TimeUtils;
import com.lzb.newloan.xlistview.XListView;


public class MoreMarketActivity extends BaseActivity implements
		 XListView.IXListViewListener {

	private XListView market_list;
	private ArrayList<MarketPojo> arrayList;
	private MarketAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_market2);
		initView();
	}

	private void initView() {
		TextView title_txt_center = (TextView) findViewById(R.id.title_txt_center);
		title_txt_center.setText("贷款超市");
		RelativeLayout backpress = (RelativeLayout) findViewById(R.id.backpress);
		backpress.setOnClickListener((View.OnClickListener) new View.OnClickListener() {
			public void onClick(final View view) {
				finish();
			}
		});
		market_list = (XListView) findViewById(R.id.market_list);
		market_list.setXListViewListener(this);
		market_list.setPullLoadEnable(false);
		market_list.setPullRefreshEnable(true);

		arrayList = new ArrayList<MarketPojo>();
		adapter = new MarketAdapter(this, arrayList);
		market_list.setAdapter(adapter);
		HttpUtils.doPostAsyn(Config.MOREMARKET_CORD,"", mHandler,
				Config.CODE_MOREMARKET);
		
		System.out.println(Config.URL + Config.MOREMARKET_CORD);
	}

	private Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case Config.CODE_URL_ERROR:
				Toast.makeText(MoreMarketActivity.this, "url错误",
						Toast.LENGTH_SHORT).show();
				break;
			case Config.CODE_NET_ERROR:
				Toast.makeText(MoreMarketActivity.this, "网络错误",
						Toast.LENGTH_SHORT).show();
				break;

			case Config.CODE_MOREMARKET:
				String result = msg.obj.toString();
				try {
					JSONObject results = new JSONObject(result);
					JSONObject page = results.getJSONObject("page");
					JSONArray array = page.getJSONArray("data");
					if (array.length() == 0) {
						showDialog("暂无数据！");
						
					} else {
						for (int i = 0; i < array.length(); i++) {
							JSONObject object = (JSONObject) array.get(i);
							MarketPojo pojo = new MarketPojo();
							pojo.setLogo(object.getString("imgurl"));
							pojo.setTitle(object.getString("title"));
							pojo.setAdvantage(object.getString("introduce"));
							pojo.setPlatform_id(object.getString("id"));
							pojo.setUrl(object.getString("url"));
							arrayList.add(pojo);
						}
						adapter.setArrayList(arrayList);
						adapter.notifyDataSetChanged();

					}
					//
				} catch (JSONException e) {
					Toast.makeText(MoreMarketActivity.this, "数据解析错误",
							Toast.LENGTH_SHORT).show();
					e.printStackTrace();
				}

				break;
			default:
				break;
			}
		};
	};



	@Override
	public void processMessage(Message message) {
		// TODO Auto-generated method stub

	}

	private int curPage = 1;
	private boolean isloadMore;

	@Override
	public void onRefresh() {
		mHandler.postDelayed(new Runnable() {
			@Override
			public void run() {
				// list.clear();
				curPage = 1;
				isloadMore = false;
				// HttpUtils.doGetAsyn(Config.MONEYRECORDE_CORD + "&userid=" +
				// userId+"&curPage="+curPage, mHandler,
				// Config.CODE_MONEYRECORDE);
				onLoad();
			}
		}, 2000);
	}

	@Override
	public void onLoadMore() {
		mHandler.postDelayed(new Runnable() {
			@Override
			public void run() {
				curPage += 1;
				isloadMore = true;
				// HttpUtils.doGetAsyn(Config.MONEYRECORDE_CORD + "&userid=" +
				// userId+"&curPage="+curPage, mHandler,
				// Config.CODE_MONEYRECORDE);
				onLoad();
			}
		}, 2000);
	}

	protected void onLoad() {
		market_list.stopRefresh();
		market_list.stopLoadMore();
		market_list.setRefreshTime(TimeUtils.getDate());
	}

	private void showDialog(String Message) {
		// TODO Auto-generated method stub
		new AlertDialog(MoreMarketActivity.this).builder().setMsg(Message)
				.setNegativeButton("确定", new OnClickListener() {
					@Override
					public void onClick(View v) {
						finish();
					}
				}).show();
	}
}
